package jdbc;

import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author oldliu
 * @since 1.0
 */
public class DBUtil {
    private DBUtil(){
    }
    public static   String DRIVER;
    public static   String URL;
    public static   String USER;
    public static   String PASS;
    static {
        Properties p=new Properties();
       try {
           p.load(DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
           DRIVER=p.getProperty("driver");
           URL=p.getProperty("url");
           USER=p.getProperty("user");
           PASS=p.getProperty("pass");
           Class.forName(DRIVER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        try {
            return  DriverManager.getConnection(URL, USER, PASS);
        } catch (SQLException e) {
            e.printStackTrace();//log4j
            throw new RuntimeException(e);
        }
    }
    public static void close(Connection con,Statement st,ResultSet rs){
        close(rs);
        close(st);
        close(con);
    }
    public static void close(Connection con){
        if(con!=null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement st){
        if(st!=null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(ResultSet rs){
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //通用的CUD方法
    public static int executeUpdate(String sql,Object...args){
        Connection con=getConnection();
        PreparedStatement ps=null;
        try {
            ps=con.prepareStatement(sql);
            for(int i=0;i<args.length;i++)
            {
                ps.setObject(i+1,args[i]);
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(con,ps,null);
        }
        return 0;
    }

    /**
     *
     * @param sql sql语句
     * @param c   查询的结果中的实体类型
     * @param args  查询参数
     * @param <T>
     * @return
     */
    public static <T> List<T>  query(String sql,Class<T>c,Object...args){
        Connection con = getConnection();
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<T> datas=new ArrayList<>();
        try {
            ps=con.prepareStatement(sql);
            for(int i=0;i<args.length;i++)
                ps.setObject(i+1,args[i]);
            rs=ps.executeQuery();
            ResultSetMetaData rm = rs.getMetaData();
            while (rs.next()){
                T t = c.newInstance();
                //填充t的各个属性
                for(int i=1;i<=rm.getColumnCount();i++){
                    String colName=rm.getColumnLabel(i);//列名
                    Object value=rs.getObject(colName);
                    Field f = c.getDeclaredField(colName);
                    f.setAccessible(true);
                    f.set(t,value);
                }
                datas.add(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(con,ps,rs);
        }
        return datas;
    }
}
